package org.nf.ssm.web.controller.user;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.ssm.common.ResultVO;
import org.nf.ssm.domain.User;
import org.nf.ssm.service.UserService;
import org.nf.ssm.web.controller.BaseController;
import org.nf.ssm.web.vo.PageVO;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhu
 * @date 2024-10-17
 */
@RestController
@RequiredArgsConstructor
public class UserController extends BaseController {

    private final UserService userService;

    @GetMapping("/user/getAllUser")
    public PageVO getAllUser(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize){
        PageVO vo = new PageVO();
        PageInfo<User> userPageInfo = userService.getAllUser(pageNum, pageSize);
        vo.setCode(200);
        vo.setData(userPageInfo);
        vo.setPageNum(userPageInfo.getPageNum());
        vo.setPageSize(userPageInfo.getPageSize());
        vo.setTotal(userPageInfo.getTotal());
        return vo;
    }

    @PostMapping("/user/addUser")
    public ResultVO addUser(@RequestBody User user){
        userService.addUser(user);
        return success();
    }

    @PostMapping("/user/updateUser")
    public ResultVO updateUser(@RequestBody User user){
        userService.updateUser(user);
        return success();
    }

    @PostMapping("/user/deleteUser")
    public ResultVO deleteUser(Integer userId){
        userService.deleteUser(userId);
        return success();
    }

}
